<template>
  <view class="container">
    <block v-if="!isLoading">
      <view class="log-list" v-if="!isEmpty">

        <view class="coupons" v-for="(item,index) in coupons" :style="'background-image: url('+$xyfun.image(item.coupons.bg_image)+');'+(item.nums==0?'filter:grayscale(100%)':'')">
          <image mode="widthFix" :src="$xyfun.image(item.coupons.image)"></image>
          <view class="info">
            <view class="title">{{item.coupons.name}}</view>
            <view class="price">￥{{item.price}}
              <text class="orignal">￥{{item.orginal_price}}</text>
            </view>
          </view>
          <view class="nums">
            <view>x{{item.nums}}</view>
            <text>/ {{item.buyed}}</text>
          </view>
        </view>

      </view>
      <view v-else>
        <xy-empty text="暂无数据" />
      </view>
    </block>
  </view>
</template>

<script>
import { mapState } from 'vuex';
import xyEmpty from '@/components/xy-empty';
export default {
  components: {
    xyEmpty,
  },
  data() {
    return {
      type : '',
      isLoading:true,
      isEmpty: true,
      coupons: [],
      currentPage: 1,
      lastPage: 1,
      loadStatus: 'loadmore',
    }
  },
  computed: {
    ...mapState(['common'])
  },
  async onLoad(options) {
    if(options?.type){
      this.type = options.type;
    }
    this.loadData();
  },
  onPullDownRefresh() {
    this.currentPage = 1;
    this.coupons = [];
    this.loadData();
  },
  onReachBottom() {
    if(this.currentPage < this.lastPage) {
      this.currentPage += 1;
      this.loadData();
    }
  },
  methods: {
    async loadData(){

      this.$api.postBase({
        url: '/coupons/my',
        loadingTip:'加载中...',
        data: {
          type : this.type,
          page: this.currentPage,
        },
        success: res => {
          uni.stopPullDownRefresh();
          this.isLoading = false;
          this.coupons = [...this.coupons, ...res.data];
          this.isEmpty = !this.coupons.length;
          this.currentPage = res.current_page;
          this.lastPage = res.last_page;
          this.loadStatus = this.currentPage < res.last_page ? 'loadmore' : 'nomore';
        }
      });
    },

  }
}
</script>

<style scoped lang="scss">
.container{
  padding-top: 30rpx;
}
.coupons{
  width: 700rpx;
  height: 134rpx;
  background-size:100% 100%;   /* 背景图片按比例缩放，覆盖整个元素 */
  display: flex;
  margin-left: 20rpx;
  margin-bottom: 20rpx;
  image{
    width: 110rpx;
    height: 110rpx;
    margin-top: 20rpx;
    margin-left: 10rpx;
  }
  .info{
    display: grid;
    place-items: baseline;
    margin-top: 10rpx;
    margin-left: 60rpx;
    .title{
      font-weight: bold;
    }
    .price{
      color: red;
      .orignal{
        color: #7a7a7a;
        text-decoration: line-through;
        font-size: 26rpx;
        margin-left: 15rpx;
      }
    }
  }
  .nums{
    height: 34rpx;
    display: flex;
    margin-left: auto;
    margin-top: 50rpx;
    margin-right: 20rpx;
    view{
      font-size: 28rpx;
      line-height: 33rpx;
      margin: 0 15rpx;
      font-weight: bold;
      color: red;
    }
    text{
      position: relative;
      top: 4rpx;
      left: -4rpx;
      color: #949494;
      font-size: 24rpx;
    }
  }
}
</style>